package com.neu.commons.Utils;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Excelutil {
    private static   String[]cols={"c_id","stu_no","stu_name","stu_sex","stu_tel",
    "stu_email","stu_pwd","stu_statis","dor_id"};
    public static List<Map<String,String>> readExcel(InputStream in) throws IOException {
        List<Map<String,String>> list=new ArrayList<>();
        //我们要读取excel文件
        XSSFWorkbook work=new XSSFWorkbook(in);
        XSSFSheet sheet=work.getSheetAt(0);
        //获取中的行数
        int rows=sheet.getPhysicalNumberOfRows();
        for(int i=1;i<rows;i++){
            XSSFRow row=sheet.getRow(i);
            Map<String,String> item=new HashMap<>();
            for(int j=0;j<cols.length;j++){
                String key=cols[j];
                //从下标为1 的列开始获取数据
                XSSFCell cell=row.getCell(j+1);//序列跳过
                String value="";
                if(cell!=null){
                    //必须要考虑每一个单元格中的数据类型
//                    if(cell.getCellTypeEnum()==CellType.NUMERIC){
//                        value+=cell.getNumericCellValue();
//                    }else{
//                        value=cell.getStringCellValue();
//                    }
                    value=cell.toString();
                }
                item.put(key,value);
            }
            list.add(item);
        }
        return list;
    }

}
